﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

using TempEmployees.DAL;

namespace TempEmployees.Web
{
    public partial class WebFormLabsUpdate : ABasePage
    {
        private int SemesterId { get { return int.Parse(SemestersDropDown.SelectedValue); } }

        protected override void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DbServices.SetDropDownSemesters(SemestersDropDown, true);
            }
        }

        protected void GenerateDepartmentsInfo()
        {
            using (TempEmpDatabaseDataContext db = DbServices.db)
            {
                List<DepartmentsInfo> list = new List<DepartmentsInfo>();
                foreach (Department d in DbServices.db.Departments)
                {
                    if (db.DepartmentsInfos.Where(dep => dep.DepartmentID == d.ID && dep.SemesterID == SemesterId).Count() > 0) continue;
                    db.DepartmentsInfos.InsertOnSubmit(new DepartmentsInfo(d.ID, SemesterId));
                }
                db.SubmitChanges();
            }
            //DepartmentsInfoGridView.DataBind();
        }

        protected void DepartmentsInfoDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            e.Result = (
                from info in DbServices.db.DepartmentsInfos
                where info.SemesterID == SemesterId
                select info);
            }

        protected void SemestersDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            GenerateDepartmentsInfo();
        }
    }
}
